package com.zbs.framework.dao.passport.normal.service.impl;

import com.zbs.framework.common.utils.CommonUtil;
import com.zbs.framework.common.utils.Constant;
import com.zbs.framework.common.utils.ErrEnum;
import com.zbs.framework.common.utils.ErrorAssert;
import com.zbs.framework.dao.passport.base.repository.MNAccountRepository;
import com.zbs.framework.dao.passport.base.service.LoginRecordService;
import com.zbs.framework.dao.passport.entity.MNAccount;
import com.zbs.framework.dao.passport.entity.MNLoginRecord;
import com.zbs.framework.dao.passport.entity.MNNormal;
import com.zbs.framework.dao.passport.normal.repository.MNNormalRepository;
import com.zbs.framework.dao.passport.normal.service.NormalAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@Transactional
public class NormalAccountServiceImpl implements NormalAccountService {

    @Autowired
    private MNNormalRepository normalRepository;

    @Autowired
    private LoginRecordService loginRecordService;

    @Autowired
    private MNAccountRepository accountRepository;

    @Override
    public MNLoginRecord login(String username, String pwd, String entryType) {
        // 参数检查
        ErrorAssert.hasLength(username, ErrEnum.USERNAME_CANT_BE_EMPTY);
        ErrorAssert.hasLength(pwd, ErrEnum.PWD_CANT_BE_EMPTY);
        ErrorAssert.notNull(entryType, ErrEnum.ENTRY_TYPE_CANT_BE_NULL);
        // 获取账号信息
        MNNormal mnNormal = normalRepository.findByUsernameAndDeleted(username,Constant.YES);
        ErrorAssert.notNull(mnNormal, ErrEnum.ACCOUNT_NOT_EXIST);
        // 密码校验
        ErrorAssert.isTrue(CommonUtil.MD5_32(pwd).equals(mnNormal.getPwd()), ErrEnum.PWD_NOT_CORRECT);
        // 更新账号登录记录
        return loginRecordService.refresh(mnNormal.getAcctUuid(), entryType);
    }

    @Override
    public MNNormal create(String username, String pwd) {
        // 参数检查
        ErrorAssert.hasLength(username, ErrEnum.USERNAME_CANT_BE_EMPTY);
        ErrorAssert.hasLength(pwd, ErrEnum.PWD_CANT_BE_EMPTY);

        // 账号查重
        MNNormal exist = normalRepository.findByUsernameAndDeleted(username,Constant.YES);
        ErrorAssert.isNull(exist, ErrEnum.ACCOUNT_ALREADY_EXIST);

        // 新增account
        MNAccount account = new MNAccount(Constant.YES);
        accountRepository.save(account);

        // 新增normal账号
        MNNormal normal = new MNNormal(username, CommonUtil.MD5_32(pwd), account.getUuid(),Constant.YES);
        normalRepository.save(normal);
        return normal;
    }

    @Override
    public void forceChangePwd(String accUuid, String pwd) {
        // 参数检查
        ErrorAssert.hasLength(accUuid, "Account uuid is required!");
        ErrorAssert.hasLength(pwd, ErrEnum.PWD_CANT_BE_EMPTY);

        // 账号检查
        MNNormal normal = normalRepository.findByAcctUuidAndDeleted(accUuid,Constant.YES);
        ErrorAssert.notNull(normal, ErrEnum.ACCOUNT_NOT_EXIST);

        // 修改密码
        normal.setPwd(CommonUtil.MD5_32(pwd));
        normalRepository.save(normal);
    }

    @Override
    public void forceChangeUsername(String accUuid, String username) {
        // 参数检查
        ErrorAssert.hasLength(accUuid, "Account uuid is required!");
        ErrorAssert.hasLength(username, ErrEnum.USERNAME_CANT_BE_EMPTY);
        // 账号检查
        MNNormal normal = normalRepository.findByAcctUuidAndDeleted(accUuid,Constant.YES);
        ErrorAssert.notNull(normal, ErrEnum.ACCOUNT_NOT_EXIST);
        // 账号查重
        if (!Objects.equals(username, normal.getUsername())) {
            MNNormal exist = normalRepository.findByUsernameAndDeleted(username,Constant.YES);
            ErrorAssert.isNull(exist, ErrEnum.ACCOUNT_ALREADY_EXIST);
            // 修改账号
            normal.setUsername(username);
        }
        normalRepository.save(normal);
    }

    @Override
    public void changePwd(String accUuid, String oldPwd, String newPwd) {
        // 参数检查
        ErrorAssert.hasLength(accUuid, "Account uuid can not be empty!");
        ErrorAssert.hasLength(oldPwd, ErrEnum.PRE_PWD_CANT_BE_EMPTY);
        ErrorAssert.hasLength(newPwd, ErrEnum.NEW_PWD_CANT_BE_EMPTY);
        //
        MNNormal normal = normalRepository.findByAcctUuidAndDeleted(accUuid,Constant.YES);
        ErrorAssert.notNull(normal, ErrEnum.ACCOUNT_NOT_EXIST);

        // 检查老密码
        ErrorAssert.isTrue(normal.getPwd().equals(CommonUtil.MD5_32(oldPwd)), ErrEnum.PRE_PWD_NOT_CORRECT);

        // 设置新密码
        normal.setPwd(CommonUtil.MD5_32(newPwd));
        normalRepository.save(normal);
    }
}
